/****************************************************************
** 
** Copyright (C) 2007 Sean Collins sean@robothusbandry.com
** 
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA  02110-1301, USA.
**
** The GNU General Public License is in the file GPL.txt
**
****************************************************************/
package gds2062;

import gnu.io.RXTXPort;
import gnu.io.SerialPort;
import gnu.io.PortInUseException;
import gnu.io.UnsupportedCommOperationException;

public class SerialPortParams
{
	private String terseString = "";
	private String name;
	private int baud;
	private int parity;
	private int databits;
	private int stopbits;
	private static int[] parityConstants = {SerialPort.PARITY_MARK
	 , SerialPort.PARITY_NONE
	 , SerialPort.PARITY_ODD
	 , SerialPort.PARITY_EVEN
	 , SerialPort.PARITY_SPACE};
	private static String parityIndex = "mnoes";

	public static SerialPortParams fromString(String s)
	{
		SerialPortParams spp = new SerialPortParams();
		String[] portParam = s.split(",");
		spp.name = portParam[0];
		spp.baud = Integer.parseInt(portParam[1]);
		spp.parity = parityConstants[parityIndex.indexOf(portParam[2])];
		spp.databits = Integer.parseInt(portParam[3]);
		spp.stopbits = Integer.parseInt(portParam[4]);
		return spp;
	}
	public RXTXPort getRXTXPort() throws gnu.io.PortInUseException, UnsupportedCommOperationException
	{
		RXTXPort thePort = new RXTXPort(name);
		thePort.setSerialPortParams(baud, databits, stopbits, parity);
		thePort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
		return thePort;
	}
	public String toString()
	{ return terseString; }
}

